package principal;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class PanelBienvenida extends JPanel implements ActionListener,Runnable{
	
	private Proyecto panel;
	private JLabel	img;
	private JLabel lblCarga,lblCargaGif;
	private final int TOPE = 100;
	private Thread hilo = new Thread(this);
	private boolean sigue = true;
	private JButton btnSalto;
	private Clip clip;
	private Clip bgm;
	
	public PanelBienvenida(Proyecto p){
		panel=p; setLayout(null);
			
		lblCarga = new JLabel("0%",JLabel.CENTER);
		lblCarga.setFont(new Font("SansSerif",Font.BOLD, 12));
		lblCarga.setBounds(150,307,50,20);
		add(lblCarga);
		
		btnSalto = new JButton(new ImageIcon("imagenes/saltar.jpg"));
		btnSalto.setBounds(40,340,230,50);
		btnSalto.setContentAreaFilled(false);
		btnSalto.setBorderPainted(false);
		btnSalto.setRolloverIcon(new ImageIcon("imagenes/saltar1.png"));
		btnSalto.setVisible(false);
		btnSalto.addActionListener(this);
		add(btnSalto);
		
		lblCargaGif = new JLabel(new ImageIcon("imagenes/cargando.gif"),JLabel.CENTER);
		lblCargaGif.setBounds(105,340,72,72);
		lblCargaGif.setVisible(true);
		add(lblCargaGif);
		
		img= new JLabel(new ImageIcon("imagenes/bienvenida.jpg"),JLabel.CENTER);
		img.setBounds(0, 0, 516, 636);
		img.setVisible(true);
		add(img);
		

		try {
			
			clip = AudioSystem.getClip();
			
			clip.open(AudioSystem.getAudioInputStream(PanelBienvenida.class.getResource("Loneliness.wav")));
		} catch (UnsupportedAudioFileException ex) {
			JOptionPane.showMessageDialog(this,
					"No se soporta ese formato de audio", "Error",
					JOptionPane.ERROR_MESSAGE);
		} catch (LineUnavailableException ex) {
			JOptionPane.showMessageDialog(this,
					"No se puede abrir la linea (no hay tarjeta de sonido)",
					"Error", JOptionPane.ERROR_MESSAGE);
		} catch (IOException ex) {
			JOptionPane.showMessageDialog(this, "No se puede abrir el archivo",
					"Error", JOptionPane.ERROR_MESSAGE);
		}
		
				
		hilo.start();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnSalto){
			sigue= false;
			panel.PBienvenida.setVisible(false);
			panel.PLogin.setVisible(true);
			panel.PLogin.txtUsuario.requestFocus();
		}
	}


	public void run() {
		int i=0;
		while(sigue){
			lblCarga.setText(i+"%");
			if(i==100){
				sigue= false;
				//panel.PBienvenida.setVisible(false);
				//panel.PLogin.setVisible(true);
				btnSalto.setVisible(true);
				lblCargaGif.setVisible(false);
			}
			clip.start();
			clip.loop(Clip.LOOP_CONTINUOUSLY);	
			i++;
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				JOptionPane.showMessageDialog(null,e.getMessage());
			}
		}
			
	}
	
}
